package team18.yuejian.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import team18.yuejian.Model.CommonModel;
import team18.yuejian.R;
import team18.yuejian.RetrofitListener;
import team18.yuejian.Server.CommonServer;
import team18.yuejian.bean.NoteBean;

public class RePasswordActivity extends AppCompatActivity implements View.OnClickListener,RetrofitListener<NoteBean> {

    private EditText UserNameView,PhoneView,OldPasswordView,NewPasswordView;
    private String UserName,Phone,OldPassword,NewPassword;
    private Button ReButton;
    private ImageButton Back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_re_password);

        InitView();
        InitEvent();

    }

    public void InitView(){
        ReButton = findViewById(R.id.rpw_bt);
        Back = findViewById(R.id.rpw_back);
        UserNameView = findViewById(R.id.rpw_username);
        PhoneView = findViewById(R.id.rpw_tel);
        OldPasswordView = findViewById(R.id.rpw_newpassword);
        NewPasswordView = findViewById(R.id.rpw_makesurepassword);


    }

    public void InitEvent(){
        ReButton.setOnClickListener(this);
        Back.setOnClickListener(this);
    }

    public void click(int num){
            if (num==0){
                UserName = UserNameView.getText().toString();
                Phone = PhoneView.getText().toString();
                OldPassword = OldPasswordView.getText().toString();
                NewPassword = NewPasswordView.getText().toString();

                if (OldPassword.equals(NewPassword)) {
                    CommonModel commonModel = new CommonModel();
                    commonModel.UserRePassword(UserName,Phone,NewPassword,this);
                }else {
                    Toast.makeText(RePasswordActivity.this,"两次密码不一致",Toast.LENGTH_SHORT).show();
                }
            }else if (num==1){
                Intent intent = new Intent(RePasswordActivity.this,LoginActivity.class);
                Toast.makeText(RePasswordActivity.this,"返回到登录界面",Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rpw_bt:
                click(0); break;
            case R.id.rpw_back:
                click(1); break;

        }
    }

    @Override
    public void onSuccess(NoteBean noteBean, int flag) {
            if ("0".equals(noteBean.getError())){
                Toast.makeText(RePasswordActivity.this,noteBean.getMsg(),Toast.LENGTH_SHORT).show();
            }else if ("1".equals(noteBean.getError())){
                Toast.makeText(RePasswordActivity.this,noteBean.getMsg(),Toast.LENGTH_SHORT).show();
            }else if ("0".equals(noteBean.getStatus())){
                Toast.makeText(RePasswordActivity.this,noteBean.getMsg(),Toast.LENGTH_SHORT).show();
            }else if ("1".equals(noteBean.getStatus())){
                Toast.makeText(RePasswordActivity.this,noteBean.getMsg(),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RePasswordActivity.this,LoginActivity.class);
                startActivity(intent);
            }
    }

    @Override
    public void onFail(String msg) {

    }
}
